package com.goldgov.notifyrecord.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goldgov.conference.service.Conference;
import com.goldgov.dict.dao.DictDao;
import com.goldgov.dict.service.DictDto;
import com.goldgov.distancelearning.service.Distancelearning;
import com.goldgov.notifyrecord.dao.NotifyRecordDao;
import com.goldgov.notifyrecord.dao.NotifyTemplateDao;
import com.goldgov.notifyrecord.service.NotifyParam;
import com.goldgov.notifyrecord.service.NotifyRecord;
import com.goldgov.notifyrecord.service.NotifyRecordService;
import com.goldgov.notifyrecord.service.NotifyTemplate;
import com.goldgov.utils.DataUtil;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.JiguangPush;

/** 
 * @ClassName: NotifyRecordServiceImpl 
 * @Description: TODO
 * @author hanxiangyu
 * @date 2018年6月28日 下午3:56:29 
 */
@Service
public class NotifyRecordServiceImpl implements NotifyRecordService {

	@Autowired
	private NotifyRecordDao notifyRecordDao;
	@Autowired
	private NotifyTemplateDao notifyTemplateDao;
	
	@Autowired
	private DictDao dictDao;
	
	
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年6月28日 下午3:57:11 
	 */
	@Override
	public Boolean addNotifyRecord(NotifyRecord notifyRecord) {
		Boolean flse = false;
		int state = 0;
		for (String userId : notifyRecord.getUserIds()) {
			notifyRecord.setReceiverId(userId);
			String userName = notifyRecordDao.getUserName(userId);
			if (userName != null && !"".equals(userName)) {
				notifyRecord.setReceiverName(userName);
			}
			state = notifyRecordDao.addNotifyRecord(notifyRecord);
		}
		if (state >= 1) {
			flse = true;
		}
		return flse;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月3日 下午2:43:14 
	 */
	@Override
	public Boolean addConNotifyRecord(Conference conference) {
		Boolean flse = false;
		String modelName = notifyTemplateDao.findNotifyModelById("f61b1ecb-48f9-11e8-9704-0242ac130008");
		if(conference.getConferenceType().equals("5")) {
			modelName="远程教育";
		}else {
			if(conference.getConferenceType().contains("5")) {
				modelName+=",远程教育";
			}
		}
		NotifyRecord nr = new NotifyRecord();
		nr.setNotifySenderCode("notify");	//发送机编码
		nr.setModelId("f61b1ecb-48f9-11e8-9704-0242ac130008");//通知模型ID 三会一课
		if (modelName != null && !"".equals(modelName)) {
			nr.setModelDesc("["+modelName+"]"+conference.getConferenceName());//标题
		}else {
			nr.setModelDesc("[三会一课]"+conference.getConferenceName());//标题
		}
		nr.setSenderId(conference.getUserID());//发送人ID
		nr.setSenderName(conference.getUserName());//发送人名称
		nr.setSendTime(new Date());
		nr.setState(1);
		nr.setUserIds(conference.getUserIDs());
		nr.setReceiverState(2);//未读
		nr.setTemplateId("65b54024-74fb-11e8-953b-0242ac130004");
		nr.setVisible(1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String dateTime = conference.getConferenceBeginDate();
		int indexOf = dateTime.indexOf(".");
		if (indexOf != -1) {
			dateTime = dateTime.substring(0,indexOf);
		}
		//计算会议时长
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date beginTime;
		Date endTime;
		String time="";
		try {
			beginTime = f.parse(conference.getConferenceBeginDate());
			endTime=f.parse(conference.getConferenceEndDate());
			time=(endTime.getTime()-beginTime.getTime())/1000/60/60+"";
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Date conferenceBeginTime = DateUtil.parseDate(dateTime);
		String beginDate=sdf.format(conferenceBeginTime);
		nr.setMeetingTime(beginDate);//会议时间
		nr.setTime(time+"小时");//会议时长
		nr.setSite(conference.getConferenceSite()==null?"":conference.getConferenceSite());//会议地点
		nr.setCompere(conference.getCompere()==null?"":conference.getCompere());//主持人
		List<String> typeList = null;
		String type = "";
		nr.setReceiptRecordId(conference.getConferenceID());
		if (conference.getConferenceType() != null && !"".equals(conference.getConferenceType())) {
			typeList = Arrays.asList(conference.getConferenceType().split(","));
			List<DictDto> dictList = dictDao.findDictList("d_shyk");
			for (String string : typeList) {
				for (DictDto dictDto : dictList) {
					if (dictDto.getDictKey().equals(string)) {
						type += dictDto.getDictValue() + ",";
						break;
					}
				}
			}
			if (type != null && !"".equals(type)) {
				type = type.substring(0,type.length()-1);
			}
		}
		int state = 0;
		
		//极光推送参数
//		Set<String> jPushUserIds = new HashSet<String>();
		Map<String, String> map = new HashMap<>();
		map.put("id", conference.getConferenceID());
		map.put("noticeType", DataUtil.SHYK_ADD);//三会一课
		
		if (conference.getUserIDs() != null && conference.getUserIDs().length > 0) {
			for (String userId : conference.getUserIDs()) {
				nr.setReceiverId(userId);
				String userName = notifyRecordDao.getUserName(userId);
				if (userName != null && !"".equals(userName)) {
					nr.setReceiverName(userName);
				}else {
					nr.setReceiverName("党员");
				}
				
				//存储模板参数
				List<NotifyParam> param = new ArrayList<>();
				if (beginDate != null && !"".equals(beginDate)) {
					param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				}
				if (conference.getConferenceSite() != null && !"".equals(conference.getConferenceSite())) {
					param.add(new NotifyParam("${site}", conference.getConferenceSite(), "会议地点"));
				}else {
					param.add(new NotifyParam("${site}", "", "会议地点"));
				}
				param.add(new NotifyParam("${name}", conference.getConferenceName(), "会议名称"));
				param.add(new NotifyParam("${userName}", userName, "通知人姓名"));
				
				String con = this.getTemplateCon(nr.getTemplateId(), param);//获取模板内容
				nr.setContent(con);
				state = notifyRecordDao.addNotifyRecord(nr);
				map.put("noticeId", nr.getMesNotifyId());
				try {
//					JiguangPush.push(userId.replace("-", ""),nr.getModelDesc(),nr.getContent(), map);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}
		
		
		if (state >= 1) {
			flse = true;
		}
		return flse;
	}
	
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月3日 下午2:43:14 
	 */
	@Override
	public Boolean updateConNotifyRecord(Conference conference) {
		Boolean flse = false;
		String modelName = notifyTemplateDao.findNotifyModelById("f61b1ecb-48f9-11e8-9704-0242ac130008");
		if(conference.getConferenceType().equals("5")) {
			modelName="远程教育";
		}else {
			if(conference.getConferenceType().contains("5")) {
				modelName+=",远程教育";
			}
		}
		NotifyRecord nr = new NotifyRecord();
		nr.setNotifySenderCode("notify");	//发送机编码
		nr.setModelId("f61b1ecb-48f9-11e8-9704-0242ac130008");//通知模型ID 三会一课
		if (modelName != null && !"".equals(modelName)) {
			nr.setModelDesc("["+modelName+"]"+conference.getConferenceName() + "修改通知");//标题
		}else {
			nr.setModelDesc("[三会一课]"+conference.getConferenceName() + "修改通知");//标题
		}
		nr.setSenderId(conference.getUserID());//发送人ID
		nr.setSenderName(conference.getUserName());//发送人名称
		nr.setSendTime(new Date());
		nr.setState(1);
		nr.setUserIds(conference.getUserIDs());
		nr.setReceiverState(2);//未读
		nr.setTemplateId("65b54024-74fb-11e8-953b-0242ac130004");
		nr.setVisible(1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		String dateTime = conference.getConferenceBeginDate();
		int indexOf = dateTime.indexOf(".");
		if (indexOf != -1) {
			dateTime = dateTime.substring(0,indexOf);
		}
		//计算会议时长
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date beginTime;
		Date endTime;
		String time="";
		try {
			beginTime = f.parse(conference.getConferenceBeginDate());
			endTime=f.parse(conference.getConferenceEndDate());
			time=(endTime.getTime()-beginTime.getTime())/1000/60/60+"";
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Date conferenceBeginTime = DateUtil.parseDate(dateTime);
		String beginDate=sdf.format(conferenceBeginTime);
		nr.setMeetingTime(beginDate);//会议时间
		nr.setTime(time+"小时");//会议时长
		nr.setSite(conference.getConferenceSite()==null?"":conference.getConferenceSite());//会议地点
		nr.setCompere(conference.getCompere()==null?"":conference.getCompere());//主持人
		List<String> typeList = null;
		String type = "";
		nr.setReceiptRecordId(conference.getConferenceID());
		if (conference.getConferenceType() != null && !"".equals(conference.getConferenceType())) {
			typeList = Arrays.asList(conference.getConferenceType().split(","));
			List<DictDto> dictList = dictDao.findDictList("d_shyk");
			for (String string : typeList) {
				for (DictDto dictDto : dictList) {
					if (dictDto.getDictKey().equals(string)) {
						type += dictDto.getDictValue() + ",";
						break;
					}
				}
			}
			if (type != null && !"".equals(type)) {
				type = type.substring(0,type.length()-1);
			}
		}
		
		int state = 0;
		
		
		//极光推送参数
//		Set<String> jPushUserIds = new HashSet<String>();
		Map<String, String> map = new HashMap<>();
		map.put("id", conference.getConferenceID());
		map.put("noticeType", DataUtil.SHYK_UPDATE);//三会一课
		if (conference.getUserIDs() != null && conference.getUserIDs().length > 0) {
			for (String userId : conference.getUserIDs()) {
				nr.setReceiverId(userId);
				String userName = notifyRecordDao.getUserName(userId);
				if (userName != null && !"".equals(userName)) {
					nr.setReceiverName(userName);
				}
				//存储模板参数
				List<NotifyParam> param = new ArrayList<>();
				if (beginDate != null && !"".equals(beginDate)) {
					param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				}
				if (conference.getConferenceSite() != null && !"".equals(conference.getConferenceSite())) {
					param.add(new NotifyParam("${site}", conference.getConferenceSite(), "会议地点"));
				}else {
					param.add(new NotifyParam("${site}", "", "会议地点"));
				}
				param.add(new NotifyParam("${type}", type, "会议类型"));
				param.add(new NotifyParam("${name}", conference.getConferenceName(), "会议名称"));
				param.add(new NotifyParam("${userName}", userName, "通知人姓名"));
				
				String con = this.getTemplateCon(nr.getTemplateId(), param);//获取模板内容
				nr.setContent(con + DataUtil.NOTICE_XG);
				state = notifyRecordDao.addNotifyRecord(nr);
				map.put("noticeId", nr.getMesNotifyId());
				try {
//					JiguangPush.push(userId.replace("-", ""),nr.getModelDesc(),nr.getContent(), map);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		
		if (state >= 1) {
			flse = true;
		}
		return flse;
		
	}
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月3日 下午2:43:14 
	 */
	@Override
	public Boolean cancelConNotifyRecord(Conference conference) {
		Boolean flse = false;
		String modelName = notifyTemplateDao.findNotifyModelById("f61b1ecb-48f9-11e8-9704-0242ac130008");
		if(conference.getConferenceType().equals("5")) {
			modelName="远程教育";
		}else {
			if(conference.getConferenceType().contains("5")) {
				modelName+=",远程教育";
			}
		}
		NotifyRecord nr = new NotifyRecord();
		nr.setNotifySenderCode("notify");	//发送机编码
		nr.setModelId("f61b1ecb-48f9-11e8-9704-0242ac130008");//通知模型ID 三会一课
		
		//极光推送参数
		Map<String, String> map = new HashMap<>();
		map.put("id", conference.getConferenceID());
		if (conference.getPublishState() == 1) {
			if (modelName != null && !"".equals(modelName)) {
				nr.setModelDesc("["+modelName+"]"+conference.getConferenceName() + "取消通知");//标题
			}else {
				nr.setModelDesc("[三会一课]"+conference.getConferenceName() + "取消通知");//标题
			}
			nr.setNoticeType(new Integer(1));
			map.put("noticeType", DataUtil.SHYK_QX);//三会一课
		}else {
			if (modelName != null && !"".equals(modelName)) {
				nr.setModelDesc("["+modelName+"]"+conference.getConferenceName());//标题
			}else {
				nr.setModelDesc("[三会一课]"+conference.getConferenceName());//标题
			}
			map.put("noticeType", DataUtil.SHYK_QY);//三会一课
		}
		nr.setTemplateId("65b54024-74fb-11e8-953b-0242ac130004");
		nr.setSenderId(conference.getUserID());//发送人ID
		nr.setSenderName(conference.getUserName());//发送人名称
		nr.setSendTime(new Date());
		nr.setState(1);
		nr.setUserIds(conference.getUserIDs());
		nr.setReceiverState(2);//未读
		nr.setVisible(1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String dateTime = conference.getConferenceBeginDate();
		int indexOf = dateTime.indexOf(".");
		if (indexOf != -1) {
			dateTime = dateTime.substring(0,indexOf);
		}
		//计算会议时长
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date beginTime;
		Date endTime;
		String time="";
		try {
			beginTime = f.parse(conference.getConferenceBeginDate());
			endTime=f.parse(conference.getConferenceEndDate());
			time=(endTime.getTime()-beginTime.getTime())/1000/60/60+"";
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Date conferenceBeginTime = DateUtil.parseDate(dateTime);
		String beginDate = sdf.format(conferenceBeginTime);
		nr.setMeetingTime(beginDate);//会议时间
		nr.setTime(time+"小时");//会议时长
		nr.setSite(conference.getConferenceSite()==null?"":conference.getConferenceSite());//会议地点
		nr.setCompere(conference.getCompere()==null?"":conference.getCompere());//主持人
		List<String> typeList = null;
		String type = "";
		nr.setReceiptRecordId(conference.getConferenceID());
		if (conference.getConferenceType() != null && !"".equals(conference.getConferenceType())) {
			typeList = Arrays.asList(conference.getConferenceType().split(","));
			List<DictDto> dictList = dictDao.findDictList("d_shyk");
			for (String string : typeList) {
				for (DictDto dictDto : dictList) {
					if (dictDto.getDictKey().equals(string)) {
						type += dictDto.getDictValue() + ",";
						break;
					}
				}
			}
			if (type != null && !"".equals(type)) {
				type = type.substring(0,type.length()-1);
			}
		}
		int state = 0;
		
		
		//极光推送参数
//		Set<String> jPushUserIds = new HashSet<String>();
		if (conference.getUserIDs() != null && conference.getUserIDs().length > 0) {
			for (String userId : conference.getUserIDs()) {
				nr.setReceiverId(userId);
				String userName = notifyRecordDao.getUserName(userId);
				if (userName != null && !"".equals(userName)) {
					nr.setReceiverName(userName);
				}
				//存储模板参数
				List<NotifyParam> param = new ArrayList<>();
				if (beginDate != null && !"".equals(beginDate)) {
					param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				}
				if (conference.getConferenceSite() != null && !"".equals(conference.getConferenceSite())) {
					param.add(new NotifyParam("${site}", conference.getConferenceSite(), "会议地点"));
				}else {
					param.add(new NotifyParam("${site}", "", "会议地点"));
				}
				param.add(new NotifyParam("${type}", type, "会议类型"));
				param.add(new NotifyParam("${name}", conference.getConferenceName(), "会议名称"));
				param.add(new NotifyParam("${userName}", userName, "通知人姓名"));
				
				String con = this.getTemplateCon(nr.getTemplateId(), param);//获取模板内容
				if (conference.getPublishState() == 1) {
					con += DataUtil.NOTICE_QX;
				}else {
					con += DataUtil.NOTICE_QY;
				}
				nr.setContent(con);
				state = notifyRecordDao.addNotifyRecord(nr);
				map.put("noticeId", nr.getMesNotifyId());
				try {
//					JiguangPush.push(userId.replace("-", ""),nr.getModelDesc(),nr.getContent(), map);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}
		
		if (state >= 1) {
			flse = true;
		}
		return flse;
		
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月3日 下午5:17:22 
	 */
	@Override
	public Boolean cancelDisNotifyRecord(Distancelearning dis) {
		String modelName = notifyTemplateDao.findNotifyModelById("f61b1ecb-48f9-11e8-9704-0242ac130009");
		Boolean flse = false;
		NotifyRecord nr = new NotifyRecord();
		nr.setNotifySenderCode("notify");	//发送机编码
		nr.setModelId("f61b1ecb-48f9-11e8-9704-0242ac130009");//通知模型ID 远程教育
		
		//极光推送参数
		Map<String, String> map = new HashMap<>();
		map.put("id", dis.getDistancelearningID());
		if (dis.getPublishState() == 1) {
			if (modelName != null && !"".equals(modelName)) {
				nr.setModelDesc("["+modelName+"]"+dis.getDistancelearningName());//标题
			}else {
				nr.setModelDesc("[远程教育]"+dis.getDistancelearningName());//标题
			}
			map.put("noticeType", DataUtil.YCJY_QY);//远程教育
		}else {
			if (modelName != null && !"".equals(modelName)) {
				nr.setModelDesc("["+modelName+"]"+dis.getDistancelearningName() + "取消通知");//标题
			}else {
				nr.setModelDesc("[远程教育]"+dis.getDistancelearningName() + "取消通知");//标题
			}
			map.put("noticeType", DataUtil.YCJY_QX);//远程教育
			nr.setNoticeType(new Integer(1));
		}
		nr.setTemplateId("54f29699-7eaa-11e8-9684-0242ac130003");
		nr.setSenderId(dis.getUserID());//发送人ID
		nr.setSenderName(dis.getUserName());//发送人名称
		nr.setSendTime(new Date());
		nr.setState(1);
		nr.setUserIds(dis.getUserIDs());
		nr.setReceiverState(2);//未读
		nr.setVisible(1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String dateTime = dis.getBeginDate();
		int indexOf = dateTime.indexOf(".");
		if (indexOf != -1) {
			dateTime = dateTime.substring(0,indexOf);
		}
		//计算会议时长
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date beginTime;
		Date endTime;
		String time="";
		try {
			beginTime = f.parse(dis.getBeginDate());
			endTime=f.parse(dis.getEndDate());
			time=(endTime.getTime()-beginTime.getTime())/1000/60/60+"";
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Date begin = DateUtil.parseDate(dateTime);
		String beginDate = sdf.format(begin);
		nr.setReceiptRecordId(dis.getDistancelearningID());
		nr.setMeetingTime(beginDate);//会议时间
		nr.setTime(time+"小时");//会议时长
		nr.setSite(dis.getDistancelearningSite()==null?"":dis.getDistancelearningSite());//会议地点
		nr.setCompere(dis.getCompere()==null?"":dis.getCompere());//
		List<String> typeList = null;
		String type = "";
		if (dis.getDistancelearningType() != null && !"".equals(dis.getDistancelearningType())) {
			typeList = Arrays.asList(dis.getDistancelearningType().split(","));
			List<DictDto> dictList = dictDao.findDictList("d_ycjy");
			for (String string : typeList) {
				for (DictDto dictDto : dictList) {
					if (dictDto.getDictKey().equals(string)) {
						type += dictDto.getDictValue() + ",";
						break;
					}
				}
			}
			if (type != null && !"".equals(type)) {
				type = type.substring(0,type.length()-1);
			}
		}
		int state = 0;
		
		
		//极光推送参数
//		Set<String> jPushUserIds = new HashSet<String>();
		if (dis.getUserIDs() != null && dis.getUserIDs().length > 0) {
			for (String userId : dis.getUserIDs()) {
				nr.setReceiverId(userId);
				String userName = notifyRecordDao.getUserName(userId);
				if (userName != null && !"".equals(userName)) {
					nr.setReceiverName(userName);
				}else {
					userName = "党员";
				}
				//存储模板参数
				List<NotifyParam> param = new ArrayList<>();
				if (beginDate != null && !"".equals(beginDate)) {
					param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				}
				if (dis.getDistancelearningSite() != null && !"".equals(dis.getDistancelearningSite())) {
					param.add(new NotifyParam("${site}", dis.getDistancelearningSite(), "会议地点"));
				}else {
					param.add(new NotifyParam("${site}", "", "会议地点"));
				}
				param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				param.add(new NotifyParam("${site}", dis.getDistancelearningSite(), "会议地点"));
				param.add(new NotifyParam("${name}", dis.getDistancelearningName(), "会议名称"));
				param.add(new NotifyParam("${type}", type, "会议类型"));
				param.add(new NotifyParam("${userName}", userName, "通知人姓名"));
				
				String con = this.getTemplateCon(nr.getTemplateId(), param);//获取模板内容
				if (dis.getPublishState() == 2) {
					con += DataUtil.NOTICE_QX;
				}else {
					con += DataUtil.NOTICE_QY;
				}
				nr.setContent(con);
				state = notifyRecordDao.addNotifyRecord(nr);
				map.put("noticeId", nr.getMesNotifyId());
				try {
//					JiguangPush.push(userId.replace("-", ""),nr.getModelDesc(),nr.getContent(), map);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}
		if (state >= 1) {
			flse = true;
		}
		return flse;
	}
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月3日 下午5:17:22 
	 */
	@Override
	public Boolean addDisNotifyRecord(Distancelearning dis) {
		String modelName = notifyTemplateDao.findNotifyModelById("f61b1ecb-48f9-11e8-9704-0242ac130009");
		Boolean flse = false;
		NotifyRecord nr = new NotifyRecord();
		nr.setNotifySenderCode("notify");	//发送机编码
		nr.setModelId("f61b1ecb-48f9-11e8-9704-0242ac130009");//通知模型ID 远程教育
		if (modelName != null && !"".equals(modelName)) {
			nr.setModelDesc("["+modelName+"]"+dis.getDistancelearningName());//标题
		}else {
			nr.setModelDesc("[远程教育]"+dis.getDistancelearningName());//标题
		}
		nr.setSenderId(dis.getUserID());//发送人ID
		nr.setSenderName(dis.getUserName());//发送人名称
		nr.setSendTime(new Date());
		nr.setState(1);
		nr.setUserIds(dis.getUserIDs());
		nr.setReceiverState(2);//未读
		nr.setTemplateId("54f29699-7eaa-11e8-9684-0242ac130003");
		nr.setVisible(1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String dateTime = dis.getBeginDate();
		int indexOf = dateTime.indexOf(".");
		if (indexOf != -1) {
			dateTime = dateTime.substring(0,indexOf);
		}
		//计算会议时长
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date beginTime;
		Date endTime;
		String time="";
		try {
			beginTime = f.parse(dis.getBeginDate());
			endTime=f.parse(dis.getEndDate());
			time=(endTime.getTime()-beginTime.getTime())/1000/60/60+"";
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Date begin = DateUtil.parseDate(dateTime);
		String beginDate = sdf.format(begin);
		nr.setReceiptRecordId(dis.getDistancelearningID());
		nr.setMeetingTime(beginDate);//会议时间
		nr.setTime(time+"小时");//会议时长
		nr.setSite(dis.getDistancelearningSite()==null?"":dis.getDistancelearningSite());//会议地点
		nr.setCompere(dis.getCompere()==null?"":dis.getCompere());//
		List<String> typeList = null;
		String type = "";
		if (dis.getDistancelearningType() != null && !"".equals(dis.getDistancelearningType())) {
			typeList = Arrays.asList(dis.getDistancelearningType().split(","));
			List<DictDto> dictList = dictDao.findDictList("d_ycjy");
			for (String string : typeList) {
				for (DictDto dictDto : dictList) {
					if (dictDto.getDictKey().equals(string)) {
						type += dictDto.getDictValue() + ",";
						break;
					}
				}
			}
			if (type != null && !"".equals(type)) {
				type = type.substring(0,type.length()-1);
			}
		}
		int state = 0;
		
		//极光推送参数
//		Set<String> jPushUserIds = new HashSet<String>();
		Map<String, String> map = new HashMap<>();
		map.put("id", dis.getDistancelearningID());
		map.put("noticeType", DataUtil.YCJY_ADD);//远程教育
		if (dis.getUserIDs() != null && dis.getUserIDs().length > 0) {
			for (String userId : dis.getUserIDs()) {
				nr.setReceiverId(userId);
				String userName = notifyRecordDao.getUserName(userId);
				if (userName != null && !"".equals(userName)) {
					nr.setReceiverName(userName);
				}else {
					userName = "党员";
				}
				
				//存储模板参数
				List<NotifyParam> param = new ArrayList<>();
				if (beginDate != null && !"".equals(beginDate)) {
					param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				}
				if (dis.getDistancelearningSite() != null && !"".equals(dis.getDistancelearningSite())) {
					param.add(new NotifyParam("${site}", dis.getDistancelearningSite(), "会议地点"));
				}else {
					param.add(new NotifyParam("${site}", "", "会议地点"));
				}
				param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				param.add(new NotifyParam("${site}", dis.getDistancelearningSite(), "会议地点"));
				param.add(new NotifyParam("${name}", dis.getDistancelearningName(), "会议名称"));
				param.add(new NotifyParam("${type}", type, "会议类型"));
				param.add(new NotifyParam("${userName}", userName, "通知人姓名"));
				
				String con = this.getTemplateCon(nr.getTemplateId(), param);//获取模板内容
				
				nr.setContent(con);
				state = notifyRecordDao.addNotifyRecord(nr);
				//极光推送参数
				map.put("noticeId", nr.getMesNotifyId());
				try {
//					JiguangPush.push(userId.replace("-", ""),nr.getModelDesc(),nr.getContent(), map);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		
		if (state >= 1) {
			flse = true;
		}
		return flse;
	}
	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年7月3日 下午5:17:22 
	 */
	@Override
	public Boolean updateDisNotifyRecord(Distancelearning dis) {
		String modelName = notifyTemplateDao.findNotifyModelById("f61b1ecb-48f9-11e8-9704-0242ac130009");
		Boolean flse = false;
		NotifyRecord nr = new NotifyRecord();
		nr.setNotifySenderCode("notify");	//发送机编码
		nr.setModelId("f61b1ecb-48f9-11e8-9704-0242ac130009");//通知模型ID 远程教育
		if (modelName != null && !"".equals(modelName)) {
			nr.setModelDesc("["+modelName+"]"+dis.getDistancelearningName() + "修改通知");//标题
		}else {
			nr.setModelDesc("[远程教育]"+dis.getDistancelearningName() + "修改通知");//标题
		}
		nr.setSenderId(dis.getUserID());//发送人ID
		nr.setSenderName(dis.getUserName());//发送人名称
		nr.setSendTime(new Date());
		nr.setState(1);
		nr.setUserIds(dis.getUserIDs());
		nr.setReceiverState(2);//未读
		nr.setTemplateId("54f29699-7eaa-11e8-9684-0242ac130003");
		nr.setVisible(1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		
		String dateTime = dis.getBeginDate();
		int indexOf = dateTime.indexOf(".");
		if (indexOf != -1) {
			dateTime = dateTime.substring(0,indexOf);
		}
		Date conferenceBeginTime = DateUtil.parseDate(dateTime);
		String beginDate = sdf.format(conferenceBeginTime);
		nr.setReceiptRecordId(dis.getDistancelearningID());
		
		List<String> typeList = null;
		String type = "";
		if (dis.getDistancelearningType() != null && !"".equals(dis.getDistancelearningType())) {
			typeList = Arrays.asList(dis.getDistancelearningType().split(","));
			List<DictDto> dictList = dictDao.findDictList("d_ycjy");
			for (String string : typeList) {
				for (DictDto dictDto : dictList) {
					if (dictDto.getDictKey().equals(string)) {
						type += dictDto.getDictValue() + ",";
						break;
					}
				}
			}
			if (type != null && !"".equals(type)) {
				type = type.substring(0,type.length()-1);
			}
		}
		int state = 0;
		
		
		//极光推送参数
//		Set<String> jPushUserIds = new HashSet<String>();
		Map<String, String> map = new HashMap<>();
		map.put("id", dis.getDistancelearningID());
		map.put("noticeType", DataUtil.YCJY_UPDATE);//远程教育
		if (dis.getUserIDs() != null && dis.getUserIDs().length > 0) {
			for (String userId : dis.getUserIDs()) {
				nr.setReceiverId(userId);
				String userName = notifyRecordDao.getUserName(userId);
				if (userName != null && !"".equals(userName)) {
					nr.setReceiverName(userName);
				}else {
					userName = "党员";
				}
				//存储模板参数
				List<NotifyParam> param = new ArrayList<>();
				if (beginDate != null && !"".equals(beginDate)) {
					param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				}
				if (dis.getDistancelearningSite() != null && !"".equals(dis.getDistancelearningSite())) {
					param.add(new NotifyParam("${site}", dis.getDistancelearningSite(), "会议地点"));
				}else {
					param.add(new NotifyParam("${site}", "", "会议地点"));
				}
				param.add(new NotifyParam("${beginDate}", beginDate, "开始时间"));
				param.add(new NotifyParam("${site}", dis.getDistancelearningSite(), "会议地点"));
				param.add(new NotifyParam("${name}", dis.getDistancelearningName(), "会议名称"));
				param.add(new NotifyParam("${type}", type, "会议类型"));
				param.add(new NotifyParam("${userName}", userName, "通知人姓名"));
				
				String con = this.getTemplateCon(nr.getTemplateId(), param);//获取模板内容
				nr.setContent(con);
				state = notifyRecordDao.addNotifyRecord(nr);
				map.put("noticeId", nr.getMesNotifyId());
				try {
//					JiguangPush.push(userId.replace("-", ""),nr.getModelDesc(),nr.getContent(), map);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		if (state >= 1) {
			flse = true;
		}
		return flse;
	}

	/** 
	 * @Description: 获取模板内容
	 * @author hanxiangyu
	 * @date 2018年7月3日 下午6:46:35 
	 */
	@Override
	public String getTemplateCon(String id, List<NotifyParam> param) {
		NotifyTemplate notifyTemplate = notifyTemplateDao.findNotifyTemplateById(id);
		String content = "";
		if (notifyTemplate != null) {
			content = notifyTemplate.getContent();
			if (param != null && !param.isEmpty()) {
				for (NotifyParam notifyParam : param) {
					content = content.replace(notifyParam.getKey(), notifyParam.getValue());
				}
			}
		}
		return content;
	}

	@Override
	public Boolean addStudyWarning(NotifyRecord obj) {
		obj.setNotifySenderCode("notify");	//发送机编码
		obj.setSendTime(new Date());
		obj.setState(1);
		obj.setReceiverState(2);//未读
		obj.setVisible(1);
		obj.setReceipt("2");
		obj.setModelId("f61b1dbb-48f9-11e8-9704-0242ac131207");//通知模型ID 远程教育
		obj.setTemplateId("7523670e-8b03-11e8-bb5f-0242ac130005");
		String con = this.getTemplateCon(obj.getTemplateId(), null);//获取模板内容
		String modelName = notifyTemplateDao.findNotifyModelById(obj.getMesNotifyId());
		if (modelName != null && !"".equals(modelName)) {
			obj.setModelDesc(modelName);//标题
		}else {
			obj.setModelDesc("学习预警");//标题
		}
		if (modelName != null && !"".equals(modelName)) {
			obj.setContent("["+modelName+"]"+con);//内容
		}else {
			obj.setContent("[学习预警]" + con);//内容
		}
		String[] userIds = {obj.getReceiverId()};
		obj.setUserIds(userIds);
		//极光推送参数
		Set<String> jPushUserIds = new HashSet<String>();
		
		jPushUserIds.add(obj.getReceiverId().replace("-", ""));
		//极光推送参数
		Map<String, String> map = new HashMap<>();
		map.put("noticeType", DataUtil.XXYJ);//远程教育
		Boolean fals = this.addNotifyRecord(obj);
		map.put("noticeId", obj.getMesNotifyId());
		try {
//			JiguangPush.push(obj.getReceiverId().replace("-", ""),obj.getModelDesc(),obj.getContent(), map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fals;
		
	}
	
	
	
	
	
	
	
	
	
	
}
